﻿using PoliticalClassifier.Support;
using System;
using System.Data;
using System.Data.OleDb;

namespace PoliticalClassifier.DataSource
{
    /// <summary>
    /// DataSourceConnection provides a concrete implementation of the IDataSourceConnection interface.
    /// </summary>
    public class DataSourceConnection : IDataSourceConnection
    {
        #region Public Properties
        /// <summary>
        /// The data set for this connection.
        /// </summary>
        public DataSet DataSet
        {
            get { return m_dataSet; }
        }
        #endregion

        #region Public Methods
        /// <summary>
        /// Opens the given file.
        /// </summary>
        /// <param name="fileName">The path to the file to open.</param>
        /// <param name="loadResults">Any errors or warnings that occured during open.</param>
        /// <returns>Whether or not any errors occured during the open.</returns>
        public bool Open(String fileName, IDataSourceLoadResults loadResults)
        {
            bool retValue;

            InitializeConnectionString(fileName);
            retValue = InitializeDataSet(loadResults);

            return retValue;
        }

        /// <summary>
        /// Closes the connection.
        /// </summary>
        public void Close()
        {
            if (m_dataSet != null)
                m_dataSet.Dispose();

            m_dataSet = null;
        }
        #endregion

        #region Private Properties
        /// <summary>
        /// Gets the connection string.
        /// </summary>
        private String ConnectionString
        {
            get
            {
                return m_connectionString;
            }
        }
        #endregion

        #region Private Method
        /// <summary>
        /// Initializes the data set by querying the open file.
        /// </summary>
        /// <param name="loadResults">Any warnings or errors resulting from the initialization.</param>
        /// <returns>Whether or not any data was accepted.</returns>
        private bool InitializeDataSet(IDataSourceLoadResults loadResults)
        {
            int count = 0;

            try
            {
                using (OleDbDataAdapter dataAdapter = new OleDbDataAdapter("Select * from [Sheet1$]", ConnectionString))
                {
                    count = dataAdapter.Fill(m_dataSet, "VotersDataSet");
                }
            }
            catch (Exception)
            {
                loadResults.AddError(new DataSourceBadFileFormatError());
            }

            return (count > 0);
        }

        /// <summary>
        /// Initializes the connection string.
        /// </summary>
        /// <param name="fileName">The path of the file to connect to.</param>
        private void InitializeConnectionString(String fileName)
        {
            m_connectionString = String.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0}; Extended Properties=Excel 12.0;", fileName);
        }
        #endregion

        #region Private Fields
        private String m_connectionString;
        private DataSet m_dataSet = new DataSet();
        #endregion
    }
}
